<template>
	<view class="control">
		<view class="section-1">
			<view class="image">
				<view
					style="width: 100%;height: 250rpx;background-color: #EDF5F5;display: flex;lex-direction: row; justify-content: center; align-items: center;">
					<image style="width: 100%;height: 250rpx;" :src="datalist.cover" mode="aspectFit"></image>
				</view>
				<!-- <image :src="datalist.cover"></image> -->
				<view class="num"> 剩余：{{ courselist.less_stock  || '' }} </view>
			</view>
			<view class="detail">
				<view class="title">{{ datalist.name || ''  }}</view>
				<view class="titles">{{ datalist.course_name  || '' }}
					<text style="margin-left: 20rpx;">{{ datalist.area_name  || '' }}</text>
				</view>
				<view class="titles">
					<text style="margin-right: 20rpx;">{{ datalist.program_name  || '' }}</text>
					<text>{{ datalist.train_enum_name || '' }}</text>
				</view>
				<view class="tag-list flex-align" style="margin-top: 10rpx;">
					<view class="tag" v-if="trainidname1">
						{{trainidname1?trainidname1:'' }}
					</view>
					<view class="tag" v-if="trainidname2">
						{{trainidname2||'' }}
					</view>
					<view class="tag" v-if="courselist.sku_name">
						{{courselist.sku_name||'' }}
					</view>
				</view>
			</view>
		</view>

		<view class="section-2">
			<view class="detail flex-between">
				<view class="flex-align">
					<view class="price-1">￥{{ price_selling }}</view>
					<!-- <view class="price-2">{{ data.classNum }}节课</view> -->
				</view>
				<!-- <view class="price-3">剩余{{ chooseStock }}名额</view> -->
			</view>

			<view class="list-title" style="display: flex;align-items: center;justify-content: space-between;"
				@click="addStudent">
				<view class="">
					选择报名人员( <text style="color: red">已选{{ studentList.length }}</text> )
				</view>
				<!-- 剩余可选人员({{ choosenum }}) -->
				<u-icon name="plus" color="#3e80fc" label="选择"></u-icon>
			</view>
			<view style="display: flex;align-items: center; justify-content: space-between; margin-top: 30rpx;"></view>
			<view class="kong flex-align" v-if="studentList.length < 1">
				<image mode="aspectFit"
					src="https://jiangsudiantuo2022.oss-cn-hangzhou.aliyuncs.com/img/39da52a35b59c656097d7b52006f8479b6de7ba6.png">
				</image>
				<view>
					<view>报名学员资料为空</view>
					<view>请登记学员信息</view>
				</view>
			</view>
			<view class="list" v-if="studentList.length > 0">
				<scroll-view class="scroll-view_H" scroll-y="true">
					<label class="item " v-for="(item, index) in studentList" :key="index">
						<view class="flex-between" style="margin-bottom: 20rpx;">
							<view class="flex-align">
								<u--image width="50" height="50" shape="circle" :src="item.head_img">
								</u--image>
								<view class="info">
									<view class="name">{{ item.name }}</view>
									<view class="phone">{{ item.mobile }}</view>
								</view>
							</view>
							<view class="radioBox" style="padding-right: 20rpx;" @click="onedit(index, item)">
								<image class="" style="width: 26rpx;height: 26rpx;"
									src="https://jiangsudiantuo2022.oss-cn-hangzhou.aliyuncs.com/img/fe85ef51643a187a6090a929b34e93738e12e365.png" />
							</view>
						</view>
						<view class="d-flex flex-row align-center"
							v-if="item.insertedData && item.insertedData.length > 0 "
							style="margin-bottom: 20rpx;margin-left: 20rpx;background-color: rgb(253,246,236);padding: 10rpx;color: #c88500;">
							<view class="d-flex flex-row" style="margin-left: 20rpx;">
								<view class="" style="font-size: 22rpx;">
									该学员已报({{item.insertedData.length}})【{{getItemNames(item.insertedData)}}】
								</view>
							</view>
						</view>
					</label>
					<view style="width: 100%;height: 10rpx;"></view>
				</scroll-view>
			</view>
		</view>
		<view class="footer flex-align" v-if="courselist.less_stock>=studentList.length">
			<view class="btn" @click="doPay" v-if="studentList.length>0">开始报名</view>
			<view class="btn btn-disbled" v-else>开始报名</view>
		</view>
		<view class="footer flex-align" v-else>
			<view class="btn btn-disbled">开始报名</view>
		</view>
	</view>
</template>

<script>
	const venues = require("@/api/venues/venues.js");
	export default {
		data() {
			return {
				id: 0,
				data: {},
				price_selling: "0.00",
				studentList: [],
				venue_id: 0,
				checkedNum: 0,
				spec: [],
				chooseStock: 0,
				chooseStudents: [],
				students: [],
				app_height: 0,
				arrid: [],
				selectLength: 0,
				quantity: 0,
				choosenum: '',
				skucode: "",
				sku: '',
				applicantlist: [],
				applicant: '',
				train_enum_id: '',
				sku_id: '',
				datalist: {},
				venue_id: '',
				listQuery: { //分页
					pageNo: 1,
					pageSize: 6,
				},
				totalPage: '',
				occupation_relation_id: '',
				courselist: {},
				course_item_id: '',
				coach_id: '',
				trainidname1: "",
				trainidname2: "",
				venue_name: '',
				student_ids: []
			};
		},

		onLoad(options) {
			uni.removeStorageSync('students');
			let _this = this;
			uni.getSystemInfo({
				success: (res) => {
					console.log("手机可用高度:" + res.windowHeight * 2 + "rpx");
					_this.app_height = res.windowHeight * 2;
				},
			});
			this.venue_id = options.venue_id
			this.coach_id = options.coach_id
			this.venue_name = options.venue_name;
			this.trainidname1 = options.trainidname1
			this.trainidname2 = options.trainidname2
			this.course_item_id = options.course_item_id
			const selectionArrs = JSON.parse(decodeURIComponent(options.selectionArr))
			this.datalist = selectionArrs
			const courselists = JSON.parse(decodeURIComponent(options.courselist))
			this.courselist = courselists
			this.price_selling = this.courselist.price_selling
			this.occupation_relation_id = this.courselist.occupation_relation_id
		},
		onShow() {
			try {
				let that = this
				// 确保 studentList 有初始值，如果有其他异步初始化操作，在这里等待它们完成
				const storageData = uni.getStorageSync('students') || [];
				this.studentList = [
					...this.studentList,
					...storageData
				].filter((item, index, arr) =>
					arr.findIndex(v => v.id === item.id) === index
				);
				uni.setStorageSync('students', that.studentList);
				console.log(that.studentList, 'that.studentList');

				that.student_ids = that.studentList.map(obj => obj.id);
				if (that.student_ids.length > 0) {
					(async () => {
						await that.stu_ck();
						// 在这里进行视图相关的操作，例如设置一个标志位来控制视图显示
						that.isDataReady = true;
					})();
				}
				this.sku_info()
			} catch (error) {
				console.error('onShow 中发生错误:', error);
			}
		},
		watch: {
			studentList: {
				deep: true,
				handler(newList) {
					// 检查新的 studentList 中是否有 insertedData 被赋值
					const hasInsertedData = newList.some(item => item.insertedData);
					if (hasInsertedData) {
						// 这里可以考虑是否需要强制更新视图，例如使用 $forceUpdate()
						// 但一般情况下 Vue 会自动检测数据变化并更新视图
						console.log('studentList 已更新且有 insertedData 值，视图将自动更新');
					}
				}
			}
		},
		methods: {
			// getItemNames(insertedData) {
			// 	if (!insertedData) {
			// 		return '';
			// 	}
			// 	const itemNames = insertedData.map(data => data.item_name);
			// 	return itemNames.join(', ');
			// },
			getItemNames(insertedData) {
				if (!insertedData) {
					return '';
				}
				const combinedNames = insertedData.map(data => {
					const parts = [data.course_item_name, data.full_name, data.item_name].filter(Boolean);
					return parts.join(' ');
				});
				return combinedNames.join(', ');
			},
			onedit(index, item) {
				let that = this
				uni.showModal({
					title: '提示',
					content: '确定删除此项吗？',
					success: (res) => {
						if (res.confirm) {
							that.studentList.splice(index, 1);
							console.log(that.studentList, 'that.studentList111')
							uni.setStorageSync('students', that.studentList);
						}
					}
				});
			},
			async stu_ck() {
				let that = this
				try {
					const params = {
						train_id: this.datalist.id,
						student_ids: this.student_ids
					};
					const res = await venues.stu_ck(params);
					if (res.code === 1) {
						const list = res.data.list;

						that.studentList.forEach((student) => {
							const studentId = student.id;
							if (list.hasOwnProperty(studentId)) {
								student['insertedData'] = list[studentId];
							}
						});
						// 手动触发视图更新
						that.$forceUpdate();
						// 输出插入后的结果
						console.log(that.studentList, 'this.studentListthis.studentList');
					}
				} catch (error) {
					console.error('stu_ck 中发生错误:', error);
				}
			},
			async sku_info() {
				let that = this
				try {
					const params = {
						sku_id: this.courselist.id,
					};
					const res = await venues.sku_info(params);
					if (res.code === 1) {
						this.price_selling = res.data.itemInfo.price_selling
					}
				} catch (error) {
					console.error('stu_ck 中发生错误:', error);
				}
			},
			addStudent() {
				uni.navigateTo({
					url: './train_add_student?venue_id=' + this.venue_id
				})
			},

			doPay() {
				let ticketID = this.data.id;
				uni.navigateTo({
					url: `./makeOther?datalist=${encodeURIComponent(JSON.stringify(this.datalist))}` +
						"&venue_id=" + this.venue_id +
						"&coach_id=" + this.coach_id + "&course_item_id=" + this.course_item_id +
						"&selectLength=" + this.studentList.length + "&trainidname1=" + this.trainidname1 +
						"&trainidname2=" + this.trainidname2 + "&venue_name=" + this.venue_name +
						"&price_selling=" + this.price_selling + "&courselist=" +
						encodeURIComponent(JSON.stringify(this.courselist)) +
						"&students=" + encodeURIComponent(JSON.stringify(this.studentList)),
				});
			},
		},
	};
</script>

<style scoped lang="scss">
	.control {
		width: 100%;
		min-height: 100vh;
		background-color: #f2f2f2;

		.section-1 {
			width: 100%;
			padding: 20rpx 30rpx;
			box-sizing: border-box;
			background-color: #2f75fa;
			height: 230rpx;
			padding-bottom: 30rpx;
			position: relative;

			.image {
				width: 200rpx;
				height: 250rpx;
				position: absolute;
				z-index: 3;
				left: 30rpx;
				top: 0;
				border-radius: 10rpx;
				overflow: hidden;

				image {
					width: 100%;
					height: 100%;
				}

				.num {
					width: 100%;
					position: absolute;
					bottom: 0;
					left: 0;
					text-align: center;
					background-color: rgba(0, 0, 0, 0.6);
					padding: 10rpx 0;
					font-size: 20rpx;
					color: #ffffff;
				}
			}

			.detail {
				margin-left: 236rpx;

				.title {
					font-size: 28rpx;
					font-weight: bold;
					color: #ffffff;
					margin-bottom: 15rpx;
				}

				.titles {
					font-size: 24rpx;
					font-weight: bold;
					color: #ffffff;
					margin-bottom: 15rpx;
				}

				.tag-list {
					.tag {
						font-size: 22rpx;
						font-weight: 500;
						color: #ffffff;
						padding: 5rpx 10rpx;
						margin-right: 10rpx;
						background-color: #4483fb;
						border-radius: 2px;
					}
				}
			}
		}

		.section-2 {
			border-radius: 20rpx 20rpx 0px 0px;
			background-color: white;
			padding: 20rpx 30rpx 20rpx 30rpx;
			height: 77vh;
			transform: translateY(-30rpx);

			.detail {
				margin-left: 236rpx;

				.price-1 {
					color: #ff4105;
					font-size: 32rpx;
					font-weight: 700;
					margin-right: 12rpx;
				}

				.price-2 {
					color: orange;
					font-size: 20rpx;
				}

				.price-3 {
					color: orange;
					font-size: 20rpx;
				}
			}

			.list-title {
				font-size: 28rpx;
				font-weight: bold;
				color: #000000;
				margin-top: 20rpx;
			}

			.list {
				.scroll-view_H {
					height: 64vh;
				}

				.item {
					margin-bottom: 30rpx;

					.info {
						margin-left: 20rpx;

						.name {
							font-size: 26rpx;
							font-weight: 500;
							color: #333333;
						}

						.phone {
							font-size: 24rpx;
							font-weight: 500;
							color: #bebebe;
							margin-top: 10rpx;
						}
					}
				}
			}
		}

		.section-3 {
			flex-direction: column;

			text {
				margin-top: 15rpx;
				font-size: 22rpx;
				font-weight: 500;
				color: #333333;
			}
		}

		.kong {
			width: 100%;
			margin-top: 40rpx;
			justify-content: center;
			font-size: 24rpx;
			font-weight: 500;
			color: #bed2fd;

			image {
				width: 250rpx;
				height: 240rpx;
				margin-right: 30rpx;
			}
		}

		.footer {
			position: fixed;
			width: 100%;
			background-color: white;
			bottom: 0;
			left: 0;
			padding: 20rpx 25rpx 20rpx 25rpx;
			box-sizing: border-box;

			.menu {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-right: 40rpx;

				image {
					width: 35rpx;
					height: 35rpx;
				}

				text {
					color: rgb(51, 51, 51);
					font-size: 24rpx;
					font-weight: 700;
					margin-top: 13rpx;
				}
			}

			.btn {
				flex: 1;
				color: rgb(255, 255, 255);
				font-size: 30rpx;
				padding: 26rpx 0;
				border-radius: 60rpx;
				background-color: #3e80fc;
				text-align: center;
				font-weight: 700;
			}

			.btn-disbled {
				flex: 1;
				color: rgb(255, 255, 255);
				font-size: 30rpx;
				padding: 26rpx 0;
				border-radius: 60rpx;
				background-color: #bebebe;
				text-align: center;
				font-weight: 700;
			}
		}

		.checkbox .wx-checkbox-input {
			border-radius: 30upx;
			/* 圆角 */
			width: 30upx;
			/* 背景的宽 */
			height: 30upx;
			/* 背景的高 */
			border: 1px solid #ccc;
			background: #eee;
		}

		/* 复选框禁用样式 */

		.checkbox .wx-checkbox-input-disabled {
			border-radius: 30upx;
			/* 圆角 */
			width: 30upx;
			/* 背景的宽 */
			height: 30upx;
			/* 背景的高 */
			background: #f3f3f3;
			border: 1px solid rgba(201, 201, 201, 1);
		}

		/* 复选框选中后样式 */

		.checkbox .wx-checkbox-input.wx-checkbox-input-checked {
			background: rgba(255, 129, 92, 1);
			border: 1px solid rgba(255, 129, 92, 1);
		}

		/*checkbox选中后图标样式 */

		.checkbox .wx-checkbox-input.wx-checkbox-input-checked::before {
			width: 30upx;
			height: 30upx;
			line-height: 30upx;
			text-align: center;
			font-size: 24upx;
			color: #fff;
			background: transparent;
			transform: translate(-50%, -50%) scale(1);
			-webkit-transform: translate(-50%, -50%) scale(1);
		}

		/deep/ checkbox .wx-checkbox-input {
			border-radius: 50% !important;
			color: #ffffff !important;
		}

		/deep/ checkbox .wx-checkbox-input.wx-checkbox-input-checked {
			color: #fff;
			background: #2851F3;
		}

		/deep/.wx-checkbox-input.wx-checkbox-input-checked {
			border: none !important;
		}

	}
</style>